package cas.ihep.util;

/**
 * Created by chocolate on 2017/11/24.
 */
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.net.URISyntaxException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.util.StringUtils;

public class Utils{
    private static final Log LOG = LogFactory.getLog(Utils.class);

    public static Path[] getRecursivePaths(FileSystem fs, String basePath)
            throws IOException, URISyntaxException {
        List<Path> result = new ArrayList<Path>();
        basePath = fs.getUri() + basePath;
        FileStatus[] listStatus = fs.globStatus(new Path(basePath));
        for (FileStatus fstat : listStatus) {
            readSubDirectory(fstat, basePath, fs, result);
        }
        return (Path[]) result.toArray(new Path[result.size()]);
    }
    public static Path[] getRecursivePaths(FileSystem fs, List path)
            throws IOException, URISyntaxException {
        List<Path> result = new ArrayList<Path>();
        for(int i =0;i<path.size();i++){
            String basePath = fs.getUri() + path.get(i).toString();
            FileStatus[] listStatus = fs.globStatus(new Path(basePath));
            for (FileStatus fstat : listStatus) {
                readSubDirectory(fstat, basePath, fs, result);
            }
        }
        return (Path[]) result.toArray(new Path[result.size()]);
    }
    private static void readSubDirectory(FileStatus fileStatus, String basePath,
                                         FileSystem fs, List<Path> paths) throws IOException, URISyntaxException {
        if (!fileStatus.isDirectory()) {
            if(fileStatus.getLen() > 0){
                paths.add(fileStatus.getPath());
            }else{
                System.out.println("File " + fileStatus.getPath().toString()+" lenth is 0 !");
            }
        }
        else {
            String subPath = fileStatus.getPath().toString();
            // System.out.println("subDir: " + subPath);
            FileStatus[] listStatus = fs.globStatus(new Path(subPath+"/*"));
            for (FileStatus fst : listStatus) {
                readSubDirectory(fst, subPath, fs, paths);
            }
        }
    }
}
